
(defun trapezoid (f a b)
  (* (- b a) (/ (+ (funcall f a) (funcall f b)) 2.0)))

(defun simpson (f a b)
  (* (/ (- b a) 6.0) (+ (funcall f a) (funcall f b) (* 4 (funcall f (/ (+ a b) 2))))))

#|
(defun integral (f a b &optional (eps 0.001))
  (if (< (- b a) eps)
      (trapezoid f a b)
      (let ((m (/ (+ a b) 2)))
        (+ (integral f a m eps) (integral f m b eps)))))
|#

(defun integral (method f a b &optional (eps 0.001))
  (if (< (- b a) eps)
      (funcall method f a b)
      (let ((m (/ (+ a b) 2)))
        (+ (integral method f a m eps) 
           (integral method f m b eps)))))

(integral #'trapezoid #'(lambda (x) (* x x)) -1 1)
(integral #'simpson #'(lambda (x) (* x x)) -1 1)


(defun qub (x) (* x x x))

(integral #'simpson #'qub 0 2.0)

(integral #'simpson #'(lambda (x) (- (* x x) (* x 3))) -2 1)


